<template>
	<view>
		<!-- <Header :isBack="true">
			<block slot="content">{{title}}</block>
		</Header> -->
		<view class="introduce-mask" v-show="showMask">
			<view class="introduce-mask-title">
				位置权限使用说明
			</view>
			<view class="introduce-mask-content">
				用于获取你的位置信息，方便展示你周围的好项目
			</view>
		</view>
		<view class="cu-bar" :style="style">
			<view class="action" @tap="BackPage" style="margin-left: 30upx;">
				<image src="@/static/images/left.png" mode="" style="width: 25upx;height:25upx;"></image>
			</view>

			<view class="search-box">
				<view class="search">
					<input class="search-input" type="text" placeholder="石材普工" v-model="keyword">
					<image class="search-img" src="@/static/images/sousuo.png" alt="" @tap="getResult"></image>
				</view>
			</view>
		</view>

		<view class="" v-if="isLoaded">
			<FilterDropdown :menuTop="menuTop" :filterData="filterData" :defaultSelected="defaultSelected"
				:updateMenuName="true" dataFormat="Object"></FilterDropdown>

			<!-- 占位 -->
			<view class="place"></view>

			<view class="list" v-if="jobData.length > 0">
				<view class="list-item" v-for="(item,index) in jobData" :key="index">
					<view class="img-wrapper">
						<view class="img-wrapper-header">
							<image class="pic" :src="item.logo">
							</image>
							<view class="img-wrapper-center">
								<text>{{item.job_title}}</text>
								<image class="img-wrapper-center-button" src="@/static/images/rijie.png" alt=""></image>
							</view>
							<view class="img-wrapper-right">
								<text class="img-wrapper-right-price">{{item.salary}}元/天</text>
								<view class="img-wrapper-right-check" @tap="checkDetail(item.id,item.job_title)"
									v-if="item.job_status == 1">
									立即预约
								</view>
								<view class="img-wrapper-right-check" @tap="checkDetail(item.id,item.job_title)"
									style="color: #fff;background-color: #999999;" v-else>
									已完工
								</view>
							</view>
						</view>

						<text class="introduce">
							{{item.description}}
						</text>
					</view>
					<view class="bot">
						<text class="bot-text">{{item.updatetime}}</text>
						<view class="bot-img-text">
							<image class="bot-img" src="/static/images/place.png"></image>
							<text class="bot-text">{{item.location}}</text>
						</view>
					</view>
				</view>

			</view>

			<view class="empty" v-else>
				<image src="/static/images/zanwushuju.png" alt="" class="empty-img"></image>
			</view>

			<view style="height:160rpx;"></view>
			<!-- 底部菜单 -->
			<BottomTabbar :selected="0"></BottomTabbar>
		</view>

	</view>

</template>

<script>
	import BottomTabbar from '@/components/bottom-tabbar/bottom-tabbar.vue';
	import FilterDropdown from '@/components/HM-filterDropdown/HM-filterDropdown.vue';
	import empty from "@/components/empty";
	import permision from "@/js_sdk/wa-permission/permission.js";
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				showMask: false,
				title: '',
				indexArr: '',
				valueArr: [],

				// #ifdef APP-PLUS
				menuTop: '160',
				// #endif

				// #ifdef H5
				menuTop: '100',
				// #endif

				// #ifdef MP-WEIXIN
				menuTop: '160',
				// #endif

				loadingText: "正在加载...",
				defaultSelected: [],
				filterData: [],

				jobData: [],
				page: 1,
				keyword: '',
				isLoaded: false,
				isShowMsg: true,

				barHeight: 0,

				type: 1,
				lat: '',
				lng: '',
			}
		},

		components: {
			// Header,
			BottomTabbar,
			FilterDropdown,
			empty
		},

		computed: {
			...mapState(['hasLogin', 'userInfo']),

			style() {
				// #ifdef APP-PLUS
				var style = `height:160rpx;padding-top:52rpx;`;
				// #endif

				// #ifdef H5
				var style = ``;
				// #endif

				// #ifdef MP-WEIXIN
				var style = `height:160rpx;padding-top:` + this.barHeight;
				// #endif

				return style
			}
		},

		// onPageScroll(event) {
		// 	if (event.scrollTop > 130) {
		// 		console.log('sdfd')
		// 		this.menuTop = 100
		// 		this.position = 'fixed'
		// 	} else {
		// 		this.menuTop = 202
		// 		this.position = 'absolute'
		// 	}
		// },

		// 页面生命周期，下拉刷新后触发
		onPullDownRefresh() {
			//判断是否登录
			if (this.type == 3) {
				let result = this.$api.isLogin();
				if (result === false) {
					return false;
				}
			}

			this.loadData()

			// 停止下拉刷新
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 500)
		},

		//下拉到底获取新的数据
		onReachBottom() {
			this.getListBySearch(this.valueArr)
		},

		onLoad: function(option) {
			this.title = option.title;
			this.type = option.type;
			//判断是否登录
			if (this.type == 3) {
				let result = this.$api.isLogin();
				if (result === false) {
					return false;
				}
			}
			uni.showLoading({
				title: '加载中...'
			});

			let height = uni.getSystemInfoSync().statusBarHeight;
			this.barHeight = height + 'px';

			this.loadData(option.keyword);
			setTimeout(() => {
				this.isLoaded = true;
				uni.hideLoading();
			}, 600)

			//筛选条件
			uni.$on('confirm', (cdata) => {
				this.keyword = ''
				this.valueArr = cdata.value;
				// console.log(this.valueArr)
				this.page = 1;
				this.getListBySearch(this.valueArr)
			})

			uni.$on('updateSendProjectsList', () => {
				if (this.page > 1) {
					--this.page;
				}

				this.isShowMsg = false;

				this.getNewJobs();
			})

			uni.$on('changeNew', () => {
				if (this.page > 1) {
					--this.page;
				}

				this.isShowMsg = false;
				this.getNewJobs();
			})
		},
		methods: {
			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			},

			async loadData(keyword = '') {

				this.page = 1;
				this.valueArr = [];
				this.keyword = '';
				let dataObj = {};
				if (keyword) {
					this.keyword = keyword;
					dataObj.keyword = keyword;
					this.valueArr.push({
						'keyword': keyword
					});
				}

				this.getAllSearch();
				this.getNewJobs(dataObj);
			},
			//获取搜索条件
			async getAllSearch() {
				let sdata = await this.$myRequest({
					url: '/common/allsearch'
				})

				let data = sdata.data.data;
				this.filterData = data;
			},
			//关键字搜索
			async getResult() {
				let keyword = this.keyword.trim();
				if (!keyword) {
					this.$api.msg('请输入搜索内容')
					this.keyword = ''
					return false;
				}

				if (this.valueArr.length > 0) {
					this.getAllSearch();
				}

				let dataObj = {
					keyword: keyword
				};

				// this.jobData = [];
				this.page = 1;
				this.valueArr.push({
					'keyword': keyword
				});
				this.getNewJobs(dataObj);
			},

			async getListBySearch(cdata) {
				let dataObj = {};
				if (cdata.length > 0) {
					cdata.forEach(function(item, index) {
						if ('keyword' in item) {
							dataObj.keyword = item.keyword;
						} else {
							switch (index) {
								case 0:
									dataObj.district = item[2] ? item[2] : item[1];
									break;
								case 1:
									dataObj.jobsort = item[2];
									break;
								case 2:
									dataObj.salary = item[0][0];
									break;
							}
						}
					});
				}

				this.getNewJobs(dataObj);
				// console.log(dataObj)
			},

			//职位列表
			async getNewJobs(dataObj = {}) {
				// console.log(dataObj)
				dataObj.page = this.page;
				dataObj.type = this.type;

				if (this.type == 2) {

					// #ifdef APP-PLUS
					let systemType = uni.getSystemInfoSync().platform;
					if (systemType === 'android') {
						// 安卓
						// uni.setStorageSync('isLocation',false)
						let isLocation = uni.getStorageSync('isLocation');
						if (!isLocation) {
							this.showMask = true;
						}
						
						console.log(isLocation)
						uni.getLocation({
							type: 'gcj02',
							success: (res) => {
								uni.setStorageSync('isLocation', true)
								dataObj.lat = res.latitude;
								dataObj.lng = res.longitude;

								this.getNewJobss(dataObj);
							},
							fail: (res) => {
								this.$api.msg('获取地址失败，将导致部分功能不可用')
							},
							complete: () => {
								this.showMask = false;
							}
						});
					} else {
						uni.setStorageSync('isLocation', false)
						uni.getLocation({
							type: 'gcj02',
							success: (res) => {
								dataObj.lat = res.latitude;
								dataObj.lng = res.longitude;

								this.getNewJobss(dataObj);
							},
							fail: (res) => {
								this.$api.msg('获取地址失败，将导致部分功能不可用')
							}
						});
					}
					// #endif
				} else {
					this.getNewJobss(dataObj);
				}
			},

			async getNewJobss(dataObj) {
				let url = '/company/jobs';

				if (this.userInfo) {
					dataObj.uid = this.userInfo.uid
				}

				if (this.type == 3) {
					url = '/user/jobs';
					dataObj.token = uni.getStorageSync('token')
				}
				let jdata = await this.$myRequest({
					url: url,
					data: dataObj,
					methods: 'post'
				})
				// console.log(jdata);
				let ndata = jdata.data.data;

				if (this.page === 1) {
					this.jobData = ndata;
					if (ndata.length === 0) {
						if (this.isShowMsg == true) {
							this.$api.msg('亲！暂无数据！！！')
						}
					} else {
						this.page = this.page + 1;
					}
				} else {
					if (ndata.length === 0) {
						if (this.jobData.length === 0) {
							if (this.isShowMsg == true) {
								this.$api.msg('亲！暂无数据！！！')
							}
						} else {
							if (this.isShowMsg == true) {
								this.$api.msg('亲！已经到底了！！！')
							}

						}

					} else {
						this.jobData = [...this.jobData, ...ndata];
						this.page = this.page + 1;
					}
				}
			},

			async checkDetail(id, title) {
				console.log(id)
				if (this.hasLogin) {
					//添加浏览记录
					let token = uni.getStorageSync('token')
					let dataObj = {
						token: token,
						job_id: id
					}

					await this.$myRequest({
						url: '/addBrowseJob',
						data: dataObj,
						methods: 'POST'
					})

					if (this.type == 3) {
						uni.$emit('changeNew')
					}
				}
				uni.navigateTo({
					url: '/pages1/pages/project-detail/project-detail?id=' + id + '&title=' + title + '详情'
				});
			}
		}
	}
</script>
<style>
	page {
		background-color: #f7f7f7;
		font-size: 30upx;
	}
</style>
<style lang='scss' scoped>
	.cu-bar {
		width: 100%;
		height: 100upx;
		display: flex;
		align-items: center;
		background-color: #f3431a;
		color: #fff;
	}

	.action {
		margin-right: 20upx;
	}

	.search-box {
		width: 80%;

		// #ifdef MP-WEIXIN
		width: 62%;
		// #endif
	}

	.search {
		height: 66rpx;
		line-height: 66rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-radius: 10rpx;
		/* margin-left: 12%; */
		background-color: #fff;
		border: 1px solid #f3f3f6;


		.search-input {
			height: 58rpx;
			font-size: 26rpx;
			padding-left: 29upx;
			background-color: #fff;
			border-radius: 23.5upx;
			width: 72%;
			color: #333;
		}

		.search-img {
			float: right;
			width: 110rpx;
			height: 58rpx;
			margin-right: 4rpx;
		}
	}

	.list {
		// #ifdef APP-PLUS
		padding: 15rpx 15rpx 15rpx;
		// #endif

		// #ifdef H5
		padding: 15rpx;
		// #endif

		// #ifdef MP-WEIXIN
		padding: 15rpx 15rpx 15rpx;
		// #endif

		.list-item {
			background-color: #fff;
			display: flex;
			flex-direction: column;
			align-items: center;
			height: 362rpx;
			border-radius: 10rpx;
			margin-bottom: 15rpx;

			.img-wrapper {
				width: 100%;
				height: 286rpx;
				position: relative;
				padding: 27rpx;
				border-bottom: 1px solid #f3f3f6;

				.img-wrapper-header {
					margin-bottom: 27rpx;
					height: 112rpx;
					position: relative;

					.pic {
						width: 112rpx;
						height: 112rpx;
						border-radius: 6upx;
						margin-right: 17rpx;
					}

					.img-wrapper-center {
						max-width: 55%;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						height: 112rpx;
						display: inline-block;
						position: absolute;
						font-size: 30rpx;

						.img-wrapper-center-button {
							width: 63rpx;
							height: 39rpx;

							display: flex;
							position: absolute;
							bottom: 0;
						}
					}

					.img-wrapper-right {
						position: absolute;
						top: 0;
						right: 0;
						display: flex;
						flex-direction: column;
						align-items: center;
					}

					.img-wrapper-right-price {
						color: #f3431a;
						font-size: 26rpx;
						/* line-height: 50rpx;
						height: 50rpx; */
					}

					.img-wrapper-right-check {
						height: 60upx;
						line-height: 60upx;
						color: #f3431a;
						width: 180upx;
						background-color: #fde4dd;
						border-top-left-radius: 30upx;
						border-bottom-left-radius: 30upx;
						font-size: 28upx;
						text-align: center;
						position: absolute;
						top: 52upx;
						right: -27upx
					}
				}

				.introduce {
					height: 96rpx;
					display: flex;
					flex-direction: column;
					line-height: 48rpx;
					font-size: $font-base !important;
					overflow: hidden;
					-webkit-line-clamp: 2;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					color: #676767;
				}
			}

			.bot {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 78rpx;
				font-size: 28upx;
				color: #676767;
				padding: 0 27rpx;
				width: 100%;


				.bot-img-text {
					display: flex;
					align-items: center;
					justify-content: space-between;
					max-width: 360rpx;

					.bot-img {
						height: 28rpx;
						width: 26rpx;
						margin-right: 14rpx;

					}

					.bot-text {
						max-width: 320rpx;
						display: inline-block;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}
		}
	}

	.datajson {
		z-index: 2;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 250px;
		padding: 0 10px;
		background-color: rgba(255, 255, 255, .8);
		backdrop-filter: blur(15px);
		-webkit-backdrop-filter: blur(15px);
		font-size: 12px;

		.text {
			margin-top: 10px;
		}

		.item {
			width: 15px;
		}
	}

	.place {
		height: 90upx;
	}

	.swiper {
		width: 100%;
		margin-top: 10upx;
		display: flex;
		justify-content: center;

		.swiper-box {
			width: 92%;
			height: 230rpx;

			overflow: hidden;
			border-radius: 15rpx;
			box-shadow: 0upx 8upx 25rpx rgba(0, 0, 0, 0.2);

			position: relative;
			z-index: 1;

			swiper {
				width: 100%;
				height: 230rpx;

				swiper-item {
					image {
						width: 100%;
						height: 230rpx;
					}
				}
			}

			.indicator {
				position: absolute;
				bottom: 20upx;
				left: 20upx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150upx;
				height: 5upx;
				border-radius: 3upx;
				overflow: hidden;
				display: flex;

				.dots {
					width: 0upx;
					background-color: rgba(255, 255, 255, 1);
					transition: all 0.3s ease-out;

					&.on {
						width: (100%/3);
					}
				}
			}
		}
	}

	.empty {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);

		.empty-img {
			width: 182rpx;
			height: 182rpx;
		}
	}

	.introduce-mask {
		position: fixed;
		z-index: 999;
		width: 86%;
		height: 150upx;
		line-height: 65upx;
		top: 100upx;
		left: 7%;
		background-color: #fff;
		padding: 10upx;
		border-radius: 20upx;
	}

	.introduce-mask-title {
		font-size: 30upx;
	}

	.introduce-mask-content {
		font-size: 28upx;
		color: #484646;
	}
</style>